<?php

/**
 * This is the model class for table "estate".
 *
 * The followings are the available columns in table 'estate':
 * @property string $id
 * @property integer $estate_id
 * @property string $title
 * @property string $address
 * @property string $location_id
 * @property string $feature_id
 * @property double $price
 * @property integer $bedrooms
 * @property integer $bathrooms
 * @property string $keyword
 * @property integer $parking_space
 * @property string $description
 * @property string $year_built
 * @property integer $sale_rent_type
 * @property string $you_are_the_id
 * @property string $property_is_for_id
 * @property string $type_of_property_id
 * @property string $in_condominuim_id
 * @property integer $status
 * @property string $created_date
 * @property integer $is_vip
 */
class Estate extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Estate the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'estate';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('estate_id, title, address, location_id, price, description, you_are_the_id, property_is_for_id, type_of_property_id, in_condominuim_id, created_date', 'required'),
			array('estate_id, bedrooms, bathrooms, parking_space, sale_rent_type, status, is_vip', 'numerical', 'integerOnly'=>true),
			array('price', 'numerical'),
			array('title', 'length', 'max'=>255),
			array('location_id, feature_id, you_are_the_id, property_is_for_id, type_of_property_id, in_condominuim_id', 'length', 'max'=>20),
			array('keyword, year_built', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, estate_id, title, address, location_id, feature_id, price, bedrooms, bathrooms, keyword, parking_space, description, year_built, sale_rent_type, you_are_the_id, property_is_for_id, type_of_property_id, in_condominuim_id, status, created_date, is_vip', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'estate_images' => array(self::BELONGS_TO, 'estate_image', 'estate_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'estate_id' => 'Estate',
			'title' => 'Title',
			'address' => 'Address',
			'location_id' => 'Location',
			'feature_id' => 'Feature',
			'price' => 'Price',
			'bedrooms' => 'Bedrooms',
			'bathrooms' => 'Bathrooms',
			'keyword' => 'Keyword',
			'parking_space' => 'Parking Space',
			'description' => 'Description',
			'year_built' => 'Year Built',
			'sale_rent_type' => 'Sale Rent Type',
			'you_are_the_id' => 'You Are The',
			'property_is_for_id' => 'Property Is For',
			'type_of_property_id' => 'Type Of Property',
			'in_condominuim_id' => 'In Condominuim',
			'status' => 'Status',
			'created_date' => 'Created Date',
			'is_vip' => 'Is Vip',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('estate_id',$this->estate_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('location_id',$this->location_id,true);
		$criteria->compare('feature_id',$this->feature_id,true);
		$criteria->compare('price',$this->price);
		$criteria->compare('bedrooms',$this->bedrooms);
		$criteria->compare('bathrooms',$this->bathrooms);
		$criteria->compare('keyword',$this->keyword,true);
		$criteria->compare('parking_space',$this->parking_space);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('year_built',$this->year_built,true);
		$criteria->compare('sale_rent_type',$this->sale_rent_type);
		$criteria->compare('you_are_the_id',$this->you_are_the_id,true);
		$criteria->compare('property_is_for_id',$this->property_is_for_id,true);
		$criteria->compare('type_of_property_id',$this->type_of_property_id,true);
		$criteria->compare('in_condominuim_id',$this->in_condominuim_id,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('is_vip',$this->is_vip);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function beforeSave()
    {
        if(parent::beforeSave())
        {
            if( $this->isNewRecord)
            $this->created_date = date('Y-m-d H:i:s');
            return true;
        }
        return false;        
    }
}